import Mustache from 'mustache'
import { getMustacheData } from './handlers'

export const piniaTs = async (apiItems: IApiItem[]) => {
    const data = await getMustacheData(apiItems)

    return Mustache.render(
        `import {
	{{#data}}
	{{methodLower}}{{funcName}}Api,
	{{/data}}
} from '@/repository'
import {
	{{#data}}
	{{#funcParamsTs}}
	I{{funcName}}ApiParams,
	{{/funcParamsTs}}
	I{{funcName}}ApiBody,
	{{/data}}
} from '@/repository/types'
import { defineStore } from 'pinia'
import { showToast } from 'vant'
import { ref } from 'vue'

export const useStore = defineStore('data', () => {
	{{#data}}
	// {{title}}
	const {{funcNameCamel}} = ref<I{{funcName}}ApiBody | null>(null)
	{{/data}}

	{{#data}}
	// 获取{{title}}
	const fetch{{funcName}} = async ({{{funcParamsTs}}}) => {
		const { code, data, msg } = await {{methodLower}}{{funcName}}Api({{{funcParamsJs}}})
		if (code !== 0) {
			showToast(msg)
			return
		}
		{{funcNameCamel}}.value = data
	}
	
	{{/data}}

	// 获取所有数据
	const getAllData = () => {
		return Promise.all([
			{{#data}}
			fetch{{funcName}}(),
			{{/data}}
		])
	}

	return {
		{{#data}}
		{{funcNameCamel}},
		{{/data}}
		{{#data}}
		fetch{{funcName}},
		{{/data}}
		getAllData,
	}
})
`,
        { data },
    )
}
